package com.ophiux.common.core.base;

import com.ophiux.common.core.enums.CodeEnum;
import com.ophiux.common.core.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel("返回结果集")
@Data
public class Result<T> {

    /**
     * 成功
     */
    public static final int SUCCESS = 0;

    /**
     * 失败
     */
    public static final int FAIL = 1;

    @ApiModelProperty("结果 0：成功并提示 100：成功不提示 1：失败  2：异常")
    private int code;

    @ApiModelProperty("提示信息")
    private String message;

    @ApiModelProperty("返回数据实体")
    private T data;

    public Result() {
        super();
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //成功
    public static <T> Result<T> success() {
        Result<T> result = new Result<T>(CodeEnum.RESULT_SUCCESS.getCode(), CodeEnum.RESULT_SUCCESS.getMsg(), null);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>(CodeEnum.RESULT_SUCCESS.getCode(), CodeEnum.RESULT_SUCCESS.getMsg(), data);
        return result;
    }

    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<T>(CodeEnum.RESULT_SUCCESS.getCode(), message, data);
        return result;
    }

    public static <T> Result<T> successMsg(String message) {
        Result<T> result = new Result<T>(CodeEnum.RESULT_SUCCESS.getCode(), message, null);
        return result;
    }

    //成功，但是不提示
    public static <T> Result<T> success_data() {
        Result<T> result = new Result<T>(CodeEnum.RESULT_SUCCESS_NOT_MESSAGE.getCode(), "", null);
        return result;
    }

    //失败
    public static <T> Result<T> fail() {
        Result<T> result = new Result<T>(CodeEnum.RESULT_FAIL.getCode(), CodeEnum.RESULT_FAIL.getMsg(), null);
        return result;
    }

    public static <T> Result<T> fail(ResultEnum resultEnum) {
        Result<T> result = new Result<T>(CodeEnum.RESULT_FAIL.getCode(), resultEnum.getMsg(), null);
        return result;
    }

    public static <T> Result<T> fail(int code, String message) {
        Result<T> result = new Result<T>(code, message, null);
        return result;
    }

    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<T>(CodeEnum.RESULT_FAIL.getCode(), message, null);
        return result;
    }

    //异常
    public static <T> Result<T> exception(ResultEnum resultEnum) {
        Result<T> result = new Result<T>(CodeEnum.RESULT_EXCEPTION.getCode(), resultEnum.getMsg(), null);
        return result;
    }

    public static <T> Result<T> exception(String message) {
        Result<T> result = new Result<T>(CodeEnum.RESULT_EXCEPTION.getCode(), message, null);
        return result;
    }

    public static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }

    public boolean isSuccess() {
        if (SUCCESS == this.code) {
            return true;
        }
        return false;
    }
}